package com.open.capacity.trade.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.open.capacity.common.exception.controller.ControllerException;
import com.open.capacity.common.exception.service.ServiceException;
import com.open.capacity.common.model.Remittance;
import com.open.capacity.common.web.PageResult;
import com.open.capacity.common.web.Result;
import com.open.capacity.trade.feign.TradeFeignClient;
import com.open.capacity.trade.model.RemittanceExcel;
import com.open.capacity.trade.service.RemittanceService;
import com.open.capacity.trade.vo.RemittanceVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Cleanup;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/remittance")
@Api(tags = "汇款管理API")
public class TradeRemittanceController {
    @Autowired
    private RemittanceService remittanceService;
    @GetMapping("/findAllRemittance")
    @ApiOperation(value = "查询所有的汇款记录，包含条件搜索")
    public PageResult<RemittanceVo> findAllRemittance(@RequestParam Map<String,Object> params) throws ControllerException{
        try{
            return remittanceService.findAllRemittance(params);
        }catch (ServiceException e){
            throw new ControllerException(e);
        }
    }
//  @PathVariable是spring3.0的一个新功能：接收请求路径中占位符的值
    @DeleteMapping("/deleteRemittance/{id}/{remittanceId}")
    @ApiOperation(value = "删除汇款管理记录")
    public Result deleteRemittance(@PathVariable Long id,@PathVariable Long remittanceId) throws ControllerException{
        try{
            remittanceService.deleteRemittance(id,remittanceId);
            return Result.succeed("操作成功");
        }catch (ServiceException e){
            throw new ControllerException(e);
        }
    }
    @PostMapping("/saveOrUpdate")
    @ApiOperation(value = "修改汇款管理")
    public Result saveOrUpdate(@RequestBody Remittance remittance) throws ControllerException{
//        是否录用与是否付款修改
        try{
            return remittanceService.saveOrUpdate(remittance);
        }catch (ServiceException e){
            throw new ControllerException(e);
        }
    }
    @PostMapping("/exportRemittance")
    @ApiOperation(value = "导出汇款管理")
    public void exportRemittance(@RequestParam Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) throws ControllerException {
        try {
            List<RemittanceExcel> result = remittanceService.findAllRemittanceExcel(params);
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-disposition", "attachment;filename=myExcel.xls");
            @Cleanup OutputStream ouputStream = null;
            Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("汇款管理导出", "汇款管理"),
                    RemittanceExcel.class, result);
            ouputStream = response.getOutputStream();
            workbook.write(ouputStream);
        } catch (ServiceException e) {
            throw new ControllerException(e);
        } catch (IOException e) {
            throw new ControllerException(e);
        }

    }

    @PostMapping("/insertRemittance")
    @ApiOperation(value = "插入汇款管理记录")
    public Result insertRemittance(@RequestBody  Remittance remittance) throws ControllerException{
        try{
            return remittanceService.insertRemittance(remittance);
        }catch (ServiceException e){
            throw new ControllerException(e);
        }
    }
    @PostMapping("/updateRemittance")
    @ApiOperation(value = "株连修改汇款管理")
    public Result updateRemittance(@RequestBody  Remittance remittance) throws ControllerException{
        try{
            return remittanceService.updateRemittance(remittance);
        }catch (ServiceException e){
            throw new ControllerException(e);
        }
    }
}
